package com.edu.test;

import com.edu.bean.Depart;
import com.edu.bean.Emp;
import com.edu.mapper.DepartMapper;
import com.edu.mapper.DepartMapper01;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test01 {

    private SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis_config.xml";
        //获取 对该资源文件的输入流
        InputStream ins = Resources.getResourceAsStream(path);
        //通过sqlSessionFactoryBuilder加载配置文件 来创建会话工厂。
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ins);
        sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void select01() {
        DepartMapper departMapper = sqlSession.getMapper(DepartMapper.class);
        Depart depart = departMapper.selectEmpById(4);
        System.out.println(depart.getDepartId());
        System.out.println(depart.getDepartName());
        System.out.println(depart.getDepartPhone());
        for (Emp emp : depart.getEmpList()) {
            System.out.println(emp);
        }
    }

    @Test
    public void select02(){
        DepartMapper mapper = sqlSession.getMapper(DepartMapper.class);
        List<Depart> departs = mapper.selectAll();
        for (Depart depart : departs) {
            System.out.println(departs);
        }
    }

    @Test
    public void select03(){
        DepartMapper01 mapper = sqlSession.getMapper(DepartMapper01.class);
        List<Depart> departList = mapper.selectAll();
        for (Depart depart : departList) {
            System.out.print(depart);
            List<Emp> empList = depart.getEmpList();
            for (Emp emp : empList) {
                System.out.println(emp);
            }
        }
    }
}
